﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BussinessObjects.BO_Actions;
using Entity.StoreProcedure;
using BusinessObjects;
using LibraryO2S;

public partial class administrator_Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["admin"] != null)
        {
            Response.Redirect("Default.aspx", false);
        }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        string username = txtUser.Value;
        string password = O2S_Encrypt.ToMD5(txtPassword.Value);
        try
        {
            BOAdminLogin _action = new BOAdminLogin();
            sp_AdminLogin _entity = new sp_AdminLogin();
            _entity.Username = username;
            _entity.Password = password;
            DataTable objDT = _action.ExecuteAndReturnDataTable(_entity);
            if (!objDT.Rows[0][0].ToString().Equals("0"))
            {
                //cookies
                //     FormsAuthentication.RedirectFromLoginPage(name.Text, true);
                FormsAuthentication.SetAuthCookie(
                     username, false);
                FormsAuthenticationTicket ticket1 =
                   new FormsAuthenticationTicket(
                        1,                                   // version
                        username,              // get username  from the form
                        DateTime.Now,                        // issue time is now
                        DateTime.Now.AddMinutes(10),         // expires in 10 minutes
                        remember.Checked,                   // cookie is not persistent    //cái này là dấu check nhớ cho lần login tiếp theo á
                        "administrator"                              // role assignment is stored in userData, cái này là thư mục User nơi chứa mấy trang aspx,cs cho User á
                        );
                HttpCookie cookie1 = new HttpCookie(
                FormsAuthentication.FormsCookieName,
                FormsAuthentication.Encrypt(ticket1));
                Response.Cookies.Add(cookie1);
                // end cookies
                if (Session["admin"] == null || Session["admin"] == "")
                    Session.Add("admin", objDT);
                else
                    Session["admin"] = objDT;
                Session.Add("first", true);
                Response.Redirect("default.aspx", false);
            }
            else
            {
                divMessages.InnerHtml = O2S_Message.Error("Lỗi Đăng Nhập", "Sai tên tài khoản hoặc mật khẩu !");
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}
